package org.yenn.pattern.factory.demo;

import lombok.extern.slf4j.Slf4j;
import org.yenn.pattern.factory.service.impl.LeftHair;
import org.yenn.pattern.factory.service.impl.RightHair;
import org.yenn.pattern.factory.service.IHair;

import java.util.Map;
import java.util.ResourceBundle;

/**
 * 发型工厂
 * @author Administrator
 *
 */
@Slf4j
public class HairFactory {

	/**
	 * 根据类型来创建对象
	 * @param key
	 * @return
	 */
	public IHair getHair(String key){
		if("left".equals(key)){
			return new LeftHair();
		}else if("right".equals(key)){
			return new RightHair();
		}
		return null;
	}
	/**
	 * 根据类的名称来生产对象
	 * @param className
	 * @return
	 */
	public IHair getHairByClass(String className){

		try {
			IHair hair = (IHair) Class.forName(className).newInstance();
			return hair;
		} catch (InstantiationException e) {
			
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 根据类的名称来生产对象
	 * @param key
	 * @return
	 */
	public IHair getHairByClassKey(String key){

		try {


			ResourceBundle message = ResourceBundle.getBundle("type");
//			log.info("message:key={},value={}",key,message.getString(key));

			IHair hair = (IHair) Class.forName(message.getString(key)).newInstance();
			return hair;
		} catch (InstantiationException e) {
			
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
		return null;
	}
}
